﻿using System;
using System.Collections.Generic;
using System.Text;
using MSXML2;
using mshtml;
using System.IO;
namespace WebHacker
{
    public class WebTools
    {
        private XMLHTTP wtXMLHTTP;

        public WebTools()
        {
            wtXMLHTTP = new XMLHTTP();
        }

        public HTMLDocument HttpRequest(String Link, RequestHeaders pRequestHeaders)
        {

            try
            {
                //Open
                this.wtXMLHTTP.open("GET", Link, false);

                //Header
                RequestHeaders.Enumerator item = pRequestHeaders.GetEnumerator();
                while (item.MoveNext())
                {

                    this.wtXMLHTTP.setRequestHeader(item.Current.Key, item.Current.Value);
                }
                
                //Send
                this.wtXMLHTTP.send();
            }catch(FileNotFoundException e){
                throw new FileNotFoundException();
                   
            }

            //404 not found

            //Check
            if (200 != this.wtXMLHTTP.status)//OK
                return null;

            return this.HTMLTextToHTMLDocument(this.wtXMLHTTP.responseText);
        }

        public String HttpRequest_Text(String Link, RequestHeaders pRequestHeaders)
        {

            try
            {
                //Open
                this.wtXMLHTTP.open("GET", Link, false);

                //Header
                RequestHeaders.Enumerator item = pRequestHeaders.GetEnumerator();
                while (item.MoveNext())
                {

                    this.wtXMLHTTP.setRequestHeader(item.Current.Key, item.Current.Value);
                }

                //Send
                this.wtXMLHTTP.send();
            }
            catch (FileNotFoundException e)
            {
                throw new FileNotFoundException();

            }

            //404 not found

            //Check
            if (200 != this.wtXMLHTTP.status)//OK
                return null;

            return this.wtXMLHTTP.responseText;
        }

        //public HTMLDocument SubmitFormData(String Link, HttpRequestDictionary vFormParas)
        //{
        public String SubmitFormDataStr(String Link, string vFormParas)
        {
            try
            {
                this.wtXMLHTTP.open("POST", Link, false);

                this.wtXMLHTTP.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
                this.wtXMLHTTP.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

                //this.wtXMLHTTP.setRequestHeader("Accept-Charset", "Shift_JIS,utf-8;q=0.7,*;q=0.3");
                //this.wtXMLHTTP.setRequestHeader("Accept-Charset", "");
                //this.wtXMLHTTP.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
                //this.wtXMLHTTP.setRequestHeader("Accept-Language", "ja");
                //this.wtXMLHTTP.setRequestHeader("Host", "pos.yotsuyaotsuka.net");
                //this.wtXMLHTTP.setRequestHeader("Origin", "http://pos.yotsuyaotsuka.net");

                this.wtXMLHTTP.send(vFormParas);

                switch (this.wtXMLHTTP.status)
                {
                    case 200:
                        //HTMLDocument _htmlDocument =
                        //this.HTMLTextToHTMLDocument(this.wtXMLHTTP.responseText);
                        //return _htmlDocument;

                        return
                            this.wtXMLHTTP.responseText;

                    //this.EncodingTest((byte[])(this.wtXMLHTTP.responseBody));
                    //this.wtXMLHTTP.getAllResponseHeaders()+"\n"+this.wtXMLHTTP.responseText;


                    // this.HTMLTextToHTMLDocument(this.wtXMLHTTP.responseText);
                    case 302://??


                        break;
                    default:
                        return null;

                }
            }
            catch (Exception e)
            {

                throw e;
            }



            return null;
        }
        public HTMLDocument SubmitFormData(String Link, string vFormParas)
        {
            try
            {
                this.wtXMLHTTP.open("POST", Link, false);

                this.wtXMLHTTP.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
                this.wtXMLHTTP.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

                //this.wtXMLHTTP.setRequestHeader("Accept-Charset", "Shift_JIS,utf-8;q=0.7,*;q=0.3");
                //this.wtXMLHTTP.setRequestHeader("Accept-Charset", "");
                //this.wtXMLHTTP.setRequestHeader("Accept-Encoding", "gzip,deflate,sdch");
                //this.wtXMLHTTP.setRequestHeader("Accept-Language", "ja");
                //this.wtXMLHTTP.setRequestHeader("Host", "pos.yotsuyaotsuka.net");
                //this.wtXMLHTTP.setRequestHeader("Origin", "http://pos.yotsuyaotsuka.net");

                this.wtXMLHTTP.send(vFormParas);
                
                switch (this.wtXMLHTTP.status)
                {
                    case 200:
                        //HTMLDocument _htmlDocument =
                        //this.HTMLTextToHTMLDocument(this.wtXMLHTTP.responseText);
                        //return _htmlDocument;

                        return
                            this.ResponseBodyToHTMLDocument(this.wtXMLHTTP.responseBody);

                            //this.EncodingTest((byte[])(this.wtXMLHTTP.responseBody));
                            //this.wtXMLHTTP.getAllResponseHeaders()+"\n"+this.wtXMLHTTP.responseText;


                    // this.HTMLTextToHTMLDocument(this.wtXMLHTTP.responseText);
                    case 302://??


                        break;
                    default:
                        return null;

                }
            }
            catch (Exception e)
            {
                
                throw e;
            }
            


            return null;
        }


        private String EncodingTest(byte[] src)
        {

            byte[] dst =
                Encoding.Convert(Encoding.GetEncoding(932), Encoding.UTF8, src);

            return
            Encoding.UTF8.GetString(dst);
        }

        private HTMLDocument ResponseBodyToHTMLDocument(object obj)
        {
            String pTextHtml = 
            Encoding.GetEncoding(932).GetString((byte[]) obj);

            try
            {
                //Cast to Html
                object[] oPageText = { pTextHtml };
                HTMLDocument doc = new HTMLDocumentClass();
                IHTMLDocument2 doc2 = (IHTMLDocument2)doc;

                doc2.designMode = "on";//http://msdn.microsoft.com/zh-cn/aa752573(v=vs.90)
                //doc2.charset = "Shift_JIS";
                doc2.write(oPageText);
                return doc2 as HTMLDocument;
            }
            catch (Exception e)
            {

                throw e;
            }

        }



        private HTMLDocument HTMLTextToHTMLDocument(String pTextHtml)
        {
            try
            {
                //Cast to Html
                object[] oPageText = { pTextHtml };
                HTMLDocument doc = new HTMLDocumentClass();
                IHTMLDocument2 doc2 = (IHTMLDocument2)doc;

                doc2.designMode = "on";
                //doc2.charset = "Shift_JIS";
                doc2.write(oPageText);
                return doc2 as HTMLDocument;
            }
            catch (Exception e)
            {
                
                throw e;
            }

        }

    }
}
